﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace Budget
{
    public static class dbData
    {
        public static List<string> listExpenseCategories = new List<string>();
        public static List<string> listIncomeCategories = new List<string>();
        public static DataTable tempCatTable;
        //stores budgeted amounts based on category
        public static Dictionary<string, decimal> CategoryBudgets = new Dictionary<string, decimal>();
        //stores if a Category is a fixed amount or not
        public static Dictionary<string, int> CategoryFixed = new Dictionary<string, int>();
        //lookups where ID is they key for a category string
        public static Dictionary<int, string> dictExpenseCategories = GetExpenseCategoryDict();
        public static Dictionary<int, string> dictIncomeCategories = GetIncomeCategoryDict();
        

        public static Dictionary<int, string> GetExpenseCategoryDict()
        {
            if (dictExpenseCategories == null)
            {
                dictExpenseCategories = new Dictionary<int, string>();
                listExpenseCategories = new List<string>();
            }
            else
            {
                return dictExpenseCategories;   
            }
            tempCatTable = DatabaseQuery.ExecStringProc(StoredProcs.getExpenseCategoryList());
            foreach (DataRow dr in tempCatTable.Rows)
            {
                dictExpenseCategories.Add(Convert.ToInt32(dr[0]), dr[1].ToString());
                listExpenseCategories.Add(dr[1].ToString());
                CategoryBudgets.Add(dr[1].ToString(), Convert.ToDecimal(dr["BudgetAmount"].ToString()));
                CategoryFixed.Add(dr[1].ToString(), Convert.ToInt16(dr["Fixed"].ToString()));
            }
            return dictExpenseCategories;
        }
        public static Dictionary<int, string> GetIncomeCategoryDict()
        {
            tempCatTable = DatabaseQuery.ExecStringProc(StoredProcs.getIncomeCategoryList());
            if (dictIncomeCategories == null)
            {
                dictIncomeCategories = new Dictionary<int, string>();
                listIncomeCategories = new List<string>();
            }
            else
            {
                return dictIncomeCategories;
            }
            foreach (DataRow dr in tempCatTable.Rows)
            {
                dictIncomeCategories.Add(Convert.ToInt32(dr[0]), dr[1].ToString());
                listIncomeCategories.Add(dr[1].ToString());
            }
            return dictIncomeCategories;
        }
        public static void GetBudgetAmounts()
        {

        }
        public static void PopulateDDLFromDictionary(Dictionary<int,string> _dict, ComboBox _ddl)
        {
            _ddl.Items.Clear();
            foreach (KeyValuePair<int, string> kvp in _dict)
            {
                _ddl.Items.Add(kvp);
            }
        }
        public static int GetIndexFromDDLBasedOnSubString(string _val, ComboBox _ddl)
        {
            string temp;
            
            for(int i=0 ; i<_ddl.Items.Count ; i++)
            {
                temp = _ddl.Items[i].ToString();
                if(temp.Contains(_val))
                {
                    return i;
                }
            }
            throw new Exception("GetIndexFromDDLBasedOnSubString Error, couldn't find value in drop down");
        }
        public static int GetCategoryIDFromCategory(string _category)
        {
            GetExpenseCategoryDict();
            foreach (KeyValuePair<int, string> kvp in dictExpenseCategories)
            {
                if (kvp.Value == _category)
                {
                    return kvp.Key;
                }
            }
            return 30;//Other
        }
        public static void Reload()
        {
            dictExpenseCategories = null;
            dictIncomeCategories = null;
            listExpenseCategories = null;
            listIncomeCategories = null;
            GetExpenseCategoryDict();
            GetIncomeCategoryDict();
        }
    }
}
